package StudyCode.summary.myfile;

import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CompressByteArrayUsingDeflater {
	public static void main(String args[]) {
		String str = "Hello World By Java";

		byte[] bytes = str.getBytes();

		Deflater deflater = new Deflater();

		deflater.setInput(bytes);
		deflater.finish();

		ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);

		byte[] buffer = new byte[1024];

		while (!deflater.finished()) {
			int bytesCompressed = deflater.deflate(buffer);
			bos.write(buffer, 0, bytesCompressed);
		}

		try {
			bos.close();
		} catch (Exception ioe) {
			ioe.printStackTrace();
		}

		byte[] compressedArray = bos.toByteArray();

		System.out.println("Byte 数组已经被压缩!");
		System.out.println("压缩前数组大小:" + bytes.length);
		System.out.println("压缩后数组大小:" + compressedArray.length);

	}
}
